package best.driver.android.View.photocrop;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Debug;
import android.os.Environment;
import android.widget.Toast;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.AppInfo;
import best.driver.android.Beans.FileInfo;
import best.driver.android.BestApp;
import best.driver.android.BuildConfig;
import best.driver.android.R;
import best.driver.android.Utils.FileImageUpload;
import best.driver.android.common.app.AppProxyFactory;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class UploadFileTask extends AsyncTask<String, Void, String> {
    //public static final String requestURL="http://192.168.208.13:8080/AndroidUploadFileWeb/FileImageUploadServlet";
    public static final String requestURL = AppProxyFactory.getProxy().getAppConfig().getApiHostUrl() +"v1/static_file/upload";

    private Activity context = null;
    private int mType;
    private FileInfo mFileInfo;
    private Bitmap mBitmap;
    private UploadListener mUploadListener;

    public UploadFileTask(Activity ctx, Bitmap bitmap, int type, UploadListener listener) {
        this.context = ctx;
        this.mType = type;
        this.mBitmap = bitmap;
        mFileInfo = new FileInfo();
        this.mUploadListener = listener;
        if (ctx instanceof BaseActivity) {
            ((BaseActivity) ctx).showWaiting("上传中...");
        }

    }

    @Override
    protected void onPostExecute(String result) {
        ((BaseActivity) context).hideWaiting();
//        if("1".equalsIgnoreCase(result)){
//        	Toast.makeText(context, "上传成功", Toast.LENGTH_LONG).show();
//        }else{
//        	Toast.makeText(context, "上传失败，请重新上传", Toast.LENGTH_LONG).show();
//        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected String doInBackground(String... params) {
        File file = new File(params[0]);
        File newFile = getFile(mBitmap);
        String status = FileImageUpload.uploadFile(newFile, requestURL, mType, mFileInfo);
        if(FileImageUpload.FAILURE.equals(status) && mUploadListener != null) {
            mUploadListener.uploadFailed();
        } else if(FileImageUpload.SUCCESS.equals(status) && mUploadListener != null) {
            mUploadListener.uploadSuccess(mFileInfo.mFileId);
        }
        return status;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    private File getFile(Bitmap bitmap) {
        File file = new File(Environment.getExternalStorageDirectory() + "/bitmap.jpg");//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
            BestApp.getInstance().getLastActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    AppInfo.showToast(BestApp.getInstance(), "存储空间可能不够，请重试");
                }
            });
            return null;
        }
        return file;
    }


}